﻿namespace LeadMaster.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using LeadMaster.Models;
    using LeadMaster.DAL;
    using LeadMaster.ViewModels;

    public class SchedulesController : Controller
    {
        private DunsmoreContext db = new DunsmoreContext();

        public ActionResult Index()
        {
            return View(db.ScheduleTemplate.ToList());
        }

        //
        // GET: /Schedules/Details/5

        public ActionResult Details(int id = 0)
        {
            ScheduleTemplate scheduletemplates = db.ScheduleTemplate.Find(id);
            if (scheduletemplates == null)
            {
                return HttpNotFound();
            }
            return View(scheduletemplates);
        }

        public ActionResult Create()
        {
            ViewModelSchedules viewModel = new ViewModelSchedules();

            return View(viewModel) ;
        }

        [HttpPost]
        public ActionResult Create(ViewModelSchedules viewModel)
        {
            if (ModelState.IsValid)
            {
                db.ScheduleTemplate.Add(viewModel.ScheduleTemplate);
                db.SaveChanges();
                foreach (ScheduleSection scheduleSection in viewModel.ScheduleSections)
                {
                    ScheduleTemplateSection scheduleTemplateSections = new ScheduleTemplateSection();
                    scheduleTemplateSections.ScheduleTemplateID = viewModel.ScheduleTemplate.ScheduleTemplateID;
                    scheduleTemplateSections.ScheduleSectionsID = scheduleSection.ScheduleSectionsID;
                    scheduleTemplateSections.DisplayOrder = scheduleSection.ScheduleSectionsID;
                    db.ScheduleTemplateSection.Add(scheduleTemplateSections);
                    db.SaveChanges();
                }
                return RedirectToAction("Index");
            }

            return View(viewModel);
        }

        public ActionResult Edit(int id = 0)
        {
            ViewModelSchedules viewModel = new ViewModelSchedules();
            viewModel.ScheduleTemplate = db.ScheduleTemplate.Find(id);
            viewModel.holdingSectionMerge = string.Empty;
            if (viewModel.ScheduleTemplate == null)
            {
                return HttpNotFound();
            }
            viewModel.ScheduleTemplateSections = db.ScheduleTemplateSection.ToList().Where(m => m.ScheduleTemplateID == id).OrderBy(m => m.DisplayOrder).ToList();
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Edit(ViewModelSchedules viewModel)
        {
            if (ModelState.IsValid)
            {
                db.Entry(viewModel.ScheduleTemplate).State = EntityState.Modified;
                foreach (ScheduleTemplateSection scheduleTemplateSection in viewModel.ScheduleTemplateSections)
                {
                    db.Entry(scheduleTemplateSection).State = EntityState.Modified;
                }
                //db.Entry(viewModel.ScheduleTemplateSections).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(viewModel);
        }

        public ActionResult Delete(int id = 0)
        {
            ScheduleTemplate scheduletemplates = db.ScheduleTemplate.Find(id);
            if (scheduletemplates == null)
            {
                return HttpNotFound();
            }
            
            foreach (ScheduleTemplateSection item in db.ScheduleTemplateSection.Where(m => m.ScheduleTemplateID == scheduletemplates.ScheduleTemplateID).ToList())
            {
                db.ScheduleTemplateSection.Remove(item);
            }
            db.ScheduleTemplate.Remove(scheduletemplates);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ScheduleTemplate scheduletemplates = db.ScheduleTemplate.Find(id);
            db.ScheduleTemplate.Remove(scheduletemplates);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult AddSection(string ScheduleTemplateID)
        {
            // Add a section 
            ViewModelSchedules viewModel = new ViewModelSchedules();
            viewModel.ScheduleSection = new ScheduleSection();
            viewModel.ScheduleTemplateID =Convert.ToInt32( ScheduleTemplateID) ;
            viewModel.ScheduleSections = db.ScheduleSection.ToList();
            return PartialView(viewModel);
        }

        [HttpPost]
        public ActionResult AddSection(ViewModelSchedules viewModel)
        {
            if (viewModel.ScheduleSectionID==0)
            {
                db.ScheduleSection.Add(viewModel.ScheduleSection);
                db.SaveChanges();
                viewModel.ScheduleSectionID = viewModel.ScheduleSection.ScheduleSectionsID;
            }
            ScheduleTemplateSection scheduleTemplateSection=new ScheduleTemplateSection() ;
            scheduleTemplateSection.ScheduleTemplateID=viewModel.ScheduleTemplateID;
            scheduleTemplateSection.ScheduleSectionsID = viewModel.ScheduleSectionID>0 ? viewModel.ScheduleSectionID : viewModel.ScheduleSection.ScheduleSectionsID;
            int nextDisplayOrder = db.ScheduleTemplateSection.Max(m => m.DisplayOrder);
            scheduleTemplateSection.DisplayOrder = ++nextDisplayOrder;
            db.ScheduleTemplateSection.Add(scheduleTemplateSection);
            db.SaveChanges();
            return RedirectToAction("Edit", new { id = viewModel.ScheduleTemplateID });
        }

        public ActionResult DeleteSection(int scheduleTemplateSectionID)
        {
            ScheduleTemplateSection scheduleTemplateSection = db.ScheduleTemplateSection.Find(scheduleTemplateSectionID);
            int scheduleTemplateID = scheduleTemplateSection.ScheduleTemplateID;
            int displayOrder = scheduleTemplateSection.DisplayOrder;
            db.ScheduleTemplateSection.Remove(scheduleTemplateSection);
            // knock all the Display Orders back 1 after this removal
            List<ScheduleTemplateSection> scheduleTemplateSections = db.ScheduleTemplateSection
                    .Where(m => m.ScheduleTemplateID == scheduleTemplateID && m.DisplayOrder > displayOrder)
                    .OrderBy(m => m.DisplayOrder)
                    .ToList();
            foreach (ScheduleTemplateSection item in scheduleTemplateSections)
            {
                item.DisplayOrder = displayOrder;
                displayOrder++;
            }
            db.SaveChanges();
            // Schedule Template ID to the Edit method
            return RedirectToAction("Edit", new { id=scheduleTemplateID });
        }
    }
}